package com.zzx.es;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;

import java.io.IOException;

/**
 * @ClassName: com.zzx.es.ESTest_Client
 * @Description: ES索引创建测试类
 * @Author zhouzhixin
 * @Date 2021/8/13
 * @Version 1.0
 */
public class ESTest_Index_Create {
    public static void main(String[] args) {
        try {
            // 连接ES客户端
            RestHighLevelClient client = new RestHighLevelClient(
                    RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))
            );
            // 创建索引
            CreateIndexRequest request = new CreateIndexRequest("user");
            CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
            // 创建结果
            boolean acknowledged = response.isAcknowledged();
            System.out.println(acknowledged);
            // 关闭ES客户端
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
